package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.MonitorLifeDataDao;
import com.jyxd.web.dao.basic.VitalSignDao;
import com.jyxd.web.dao.patient.PatientDao;
import com.jyxd.web.data.basic.VitalSign;
import com.jyxd.web.data.dto.TemperatureMomentDTO;
import com.jyxd.web.data.patient.Patient;
import com.jyxd.web.error.JyxdException;
import com.jyxd.web.service.patient.PatientScoreService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

import static com.jyxd.web.util.DateUtil.*;
import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;

@Service
@Transactional
public class VitalSignService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private VitalSignDao vitalSignDao;

    @Autowired
    private MonitorLifeDataDao monitorLifeDataDao;

    @Autowired
    private PatientDao patientDao;

    @Autowired
    private PatientScoreService patientScoreService;

    public boolean insert(VitalSign vitalSign) {
        return vitalSignDao.insert(vitalSign);
    }

    public boolean update(VitalSign vitalSign) {
        return vitalSignDao.update(vitalSign);
    }

    public VitalSign queryData(String id) {
        return vitalSignDao.queryData(id);
    }

    public List<VitalSign> queryList(Map<String, Object> map) {
        return vitalSignDao.queryList(map);
    }

    public List<Map<String, Object>> getListByStartTime(Map<String, Object> map) {
        return vitalSignDao.getListByStartTime(map);
    }

    //3,7,11,15,19,23
    public static String time1 = "03";
    public static String time2 = "07";
    public static String time3 = "11";
    public static String time4 = "15";
    public static String time5 = "19";
    public static String time6 = "23";

    public int queryNum(Map<String, Object> map) {
        return vitalSignDao.queryNum(map);
    }

    /**
     * 根据时间和code查询对象
     *
     * @param map
     * @return
     */
    public List<VitalSign> queryDataByTimeAndCode(Map<String, Object> map) {
        return vitalSignDao.queryDataByTimeAndCode(map);
    }

    /**
     * 根据时间和病人主键id查询对象列表
     *
     * @param map
     * @return
     */
    public List<VitalSign> queryListByTime(Map<String, Object> map) {
        return vitalSignDao.queryListByTime(map);
    }

    /**
     * 护理文书--护理单--生命体征--根据病人主键id 查询生命体征列表
     *
     * @return
     */
    public List<Map<String, Object>> getList(Map<String, Object> map) {
        return vitalSignDao.getList(map);
    }

    /**
     * 根据病人id获取某个时间段生命体征最大值
     *
     * @param map
     * @return
     */
    public JSONObject getMaxVitalSign(Map<String, Object> map) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> maxMap = vitalSignDao.getMaxVitalSign(map);
        if (maxMap != null) {
            jsonObject = JSONObject.fromObject(maxMap);
        }
        return jsonObject;
    }

    /**
     * 根据病人id获取某个时间段生命体征最小值
     *
     * @param map
     * @return
     */
    public JSONObject getMinVitalSign(Map<String, Object> map) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> maxMap = vitalSignDao.getMinVitalSign(map);
        if (maxMap != null) {
            jsonObject = JSONObject.fromObject(maxMap);
        }
        return jsonObject;
    }

    /**
     * 根据病人id获取某个时间生命体征值
     *
     * @param map
     * @return
     */
    public JSONObject getNowVitalSign(Map<String, Object> map) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> maxMap = vitalSignDao.getNowVitalSign(map);
        if (maxMap != null) {
            jsonObject = JSONObject.fromObject(maxMap);
        }
        return jsonObject;
    }

    /**
     * 根据病人id获取某个时间人工气道方式
     *
     * @param map
     * @return
     */
    public JSONObject getAirWay(Map<String, Object> map) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> maxMap = vitalSignDao.getAirWay(map);
        if (maxMap != null) {
            jsonObject = JSONObject.fromObject(maxMap);
        }
        return jsonObject;
    }

    public List<Map<String, Object>> getListInfo(Map<String, Object> map) {
        return vitalSignDao.getListInfo(map);
    }







    /**
     * 查询体温单中的脉搏与心率的数据
     *
     * @param patientId   患者id
     * @param dateDayList 时间年月日集合
     * @return
     */
    public List<TemperatureMomentDTO> findTemperatureData(String patientId, List<String> dateDayList) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        for (int i = 0; i < dateDayList.size(); i++) {
            String day = dateDayList.get(i);
            // TemperatureDayDTO temperatureDay = new TemperatureDayDTO();
            //temperatureDay.setDateDay(day);
            //赋值不同时刻点内的数据
            // temperatureDay.setMomentList(getTemperatureMomentDTOList(patientId, day, i));
            // temperatureDayList.add(temperatureDay);
            List<TemperatureMomentDTO> newList = getTemperatureMomentDTOList(patientId, day, i);
            temperatureDayList.addAll(newList);
        }
        return temperatureDayList;
    }

    /**
     * 查询体温单中的血压的数据
     *
     * @param patientId   患者id
     * @param dateDayList 时间年月日集合
     * @return
     */
    public List<TemperatureMomentDTO> findTemperatureBloodPressureData(String patientId, List<String> dateDayList) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        for (int i = 0; i < dateDayList.size(); i++) {
            String day = dateDayList.get(i);
            temperatureDayList.add(getBloodPressure(day, patientId));
        }
        return temperatureDayList;
    }

    /**
     * 查询患者状态描述
     *
     * @param patientId
     * @param dateDayList
     * @return
     */
    public List<TemperatureMomentDTO> findTemperatureChineseStatusData(String patientId, List<String> dateDayList) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        Patient patient = patientDao.queryData(patientId);
        if (Objects.isNull(patient)) {
            throw new JyxdException("查询患者不存在");
        }
        LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
        for (int i = 0; i < dateDayList.size(); i++) {
            String day = dateDayList.get(i);
            map.put(day, i);
        }
        List<TemperatureMomentDTO> newList = getChineseStatusTemperatureMomentDTO(patient, map);
        temperatureDayList.addAll(newList);
        return temperatureDayList;
    }

    /**
     * 获取体温单入院，出院文字描述
     *
     * @param patient
     * @return
     */
    private List<TemperatureMomentDTO> getChineseStatusTemperatureMomentDTO(Patient patient, LinkedHashMap<String, Integer> map) {
        List<TemperatureMomentDTO> temperatureMomentList = new ArrayList<>();
        Date visitTime = patient.getVisitTime();
        if (getChineseStatusDto(visitTime, map, "in", null) != null) {
            temperatureMomentList.add(getChineseStatusDto(visitTime, map, "in", null));
        }

        if (Objects.isNull(patient.getExitTime())) {
            return temperatureMomentList;
        }
        Date exitTime = patient.getExitTime();
        if (getChineseStatusDto(exitTime, map, "out", patient.getExitType()) != null) {
            temperatureMomentList.add(getChineseStatusDto(exitTime, map, "out", patient.getExitType()));
        }
        return temperatureMomentList;
    }

    /**
     * 获取体温单入院，出院文字描述
     *
     * @return
     */
    private TemperatureMomentDTO getChineseStatusDto(Date time, LinkedHashMap<String, Integer> map, String inOrOut, String exitType) {
        if (time != null) {
            String timeStr = yyyyMMddSdfToString(time);
            String exitHour = hhSdfToString(time);
            Integer i = map.get(timeStr);
            if (i != null) {
                int timeMoment = Integer.valueOf(exitHour) + (24 * i);
                String chinese = null;
                if (StringUtils.equals(inOrOut, "in")) {
                    chinese = getChinese(0, time);
                } else if (StringUtils.equals(inOrOut, "out")) {
                    switch (exitType) {
                        case "出院":
                            chinese = getChinese(1, time);
                            break;
                        case "死亡":
                            chinese = getChinese(2, time);
                            break;
                        case "转出科":
                            chinese = getChinese(3, time);
                            break;
                    }
                }
                return new TemperatureMomentDTO(timeMoment, chinese, 2);
            }
        }
        return null;
    }

    /**
     * 获取体温单入院，出院文字描述
     *
     * @param i
     * @return
     */

    private String getChinese(int i, Date visitTime) {
        StringBuffer stringBuffer = new StringBuffer();
        if (i == 0) {
            stringBuffer.append("入\n院\n");
            stringBuffer.append(hhSdfToString(visitTime));
            stringBuffer.append("\n时\n");
            stringBuffer.append(mmSdfToString(visitTime));
            stringBuffer.append("\n分");
        }
        if (i == 1) {
            stringBuffer.append("出\n院\n");
            stringBuffer.append(hhSdfToString(visitTime));
            stringBuffer.append("\n时\n");
            stringBuffer.append(mmSdfToString(visitTime));
            stringBuffer.append("\n分");
        }
        if (i == 2) {
            stringBuffer.append("死\n亡\n");
            stringBuffer.append(hhSdfToString(visitTime));
            stringBuffer.append("\n时\n");
            stringBuffer.append(mmSdfToString(visitTime));
            stringBuffer.append("\n分");
        }
        if (i == 3) {
            stringBuffer.append("转\n科\n");
            stringBuffer.append(hhSdfToString(visitTime));
            stringBuffer.append("\n时\n");
            stringBuffer.append(mmSdfToString(visitTime));
            stringBuffer.append("\n分");
        }
        return stringBuffer.toString();
    }

    /**
     * 获取体温单中疼痛评分的分值数据
     *
     * @param patientId
     * @param dateDayList
     * @return
     */
    public List<TemperatureMomentDTO> findTemperatureAcheScoreData(String patientId, List<String> dateDayList) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        for (int i = 0; i < dateDayList.size(); i++) {
            String day = dateDayList.get(i);
            List<TemperatureMomentDTO> newList = getAcheScoreDTOList(patientId, day, i);
            if (newList != null && newList.size() > 0) {
                temperatureDayList.addAll(newList);
            }
        }
        return temperatureDayList;
    }

    private List<TemperatureMomentDTO> getAcheScoreDTOList(String patientId, String day, int i) {
        List<TemperatureMomentDTO> temperatureMomentList = new ArrayList<>();
        if (getAcheScoreDTO(3 + (24 * i), day + " " + "03", patientId) != null) {
            temperatureMomentList.add(getAcheScoreDTO(3 + (24 * i), day + " " + "03", patientId));
        }
        if (getAcheScoreDTO(7 + (24 * i), day + " " + "07", patientId) != null) {
            temperatureMomentList.add(getAcheScoreDTO(7 + (24 * i), day + " " + "07", patientId));
        }
        if (getAcheScoreDTO(11 + (24 * i), day + " " + "11", patientId) != null) {
            temperatureMomentList.add(getAcheScoreDTO(11 + (24 * i), day + " " + "11", patientId));
        }
        if (getAcheScoreDTO(15 + (24 * i), day + " " + "15", patientId) != null) {
            temperatureMomentList.add(getAcheScoreDTO(15 + (24 * i), day + " " + "15", patientId));
        }
        if (getAcheScoreDTO(19 + (24 * i), day + " " + "19", patientId) != null) {
            temperatureMomentList.add(getAcheScoreDTO(19 + (24 * i), day + " " + "19", patientId));
        }
        if (getAcheScoreDTO(23 + (24 * i), day + " " + "23", patientId) != null) {
            temperatureMomentList.add(getAcheScoreDTO(23 + (24 * i), day + " " + "23", patientId));
        }
        return temperatureMomentList;
    }

    /**
     * 获取镇痛评分的数据
     *
     * @param moment
     * @param dataTime
     * @param patientId
     * @return
     */
    private TemperatureMomentDTO getAcheScoreDTO(int moment, String dataTime, String patientId) {
        if ("".equals(getScoreItemDictionary(patientId, dataTime))) {
            return null;
        }
        return new TemperatureMomentDTO(moment, getScoreItemDictionary(patientId, dataTime), 1);
    }

    /**
     * 获取疼痛评分
     *
     * @return
     */
    private String getScoreItemDictionary(String patientId, String dataTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("patientId", patientId);
        //map.put("type", "cpot");
        map.put("type", "wbs");
        map.put("dataTime", dataTime);
        List<Map<String, Object>> patientScoreList = patientScoreService.getPatientCpotScoreAndLevel(map);
        if (CollectionUtils.isEmpty(patientScoreList)) {
            return "";
        }
        Map<String, Object> patientScoreMap = patientScoreList.get(0);
        if (CollectionUtils.isEmpty(patientScoreMap)) {
            return "";
        }
        if (Objects.isNull(patientScoreMap.get("score"))) {
            return "";
        }
        return String.valueOf(patientScoreMap.get("score"));
    }

    /**
     * 查询体温单中的呼吸的数据
     *
     * @param patientId   患者id
     * @param dateDayList 时间年月日集合
     * @return
     */
    public List<TemperatureMomentDTO> findTemperatureBrData(String patientId, List<String> dateDayList) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        for (int i = 0; i < dateDayList.size(); i++) {
            String day = dateDayList.get(i);
            List<TemperatureMomentDTO> newList = getBrDTOList(patientId, day, i);
            temperatureDayList.addAll(newList);
        }
        return temperatureDayList;
    }

    private List<TemperatureMomentDTO> getBrDTOList(String patientId, String day, int i) {
        List<TemperatureMomentDTO> temperatureMomentList = new ArrayList<>();
        temperatureMomentList.add(getBrDTO(2 + (24 * i), day + " " + "02", patientId));
        temperatureMomentList.add(getBrDTO(6 + (24 * i), day + " " + "06", patientId));
        temperatureMomentList.add(getBrDTO(10 + (24 * i), day + " " + "10", patientId));
        temperatureMomentList.add(getBrDTO(14 + (24 * i), day + " " + "14", patientId));
        temperatureMomentList.add(getBrDTO(18 + (24 * i), day + " " + "18", patientId));
        temperatureMomentList.add(getBrDTO(22 + (24 * i), day + " " + "22", patientId));
        return temperatureMomentList;
    }

    /**
     * 查询体温单中的体温的数据
     *
     * @param patientId   患者id
     * @param dateDayList 时间年月日集合
     * @return
     */
    public List<TemperatureMomentDTO> findTemperatureTWData(String patientId, List<String> dateDayList) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        for (int i = 0; i < dateDayList.size(); i++) {
            String day = dateDayList.get(i);
            List<TemperatureMomentDTO> newList = getTWDTOList(patientId, day, i);
            if (newList != null && newList.size() > 0) {
                temperatureDayList.addAll(newList);
            }
        }
        return temperatureDayList;
    }

    private List<TemperatureMomentDTO> getTWDTOList(String patientId, String day, int i) {
        List<TemperatureMomentDTO> temperatureMomentList = new ArrayList<>();
        if (getTWDTO(2 + (24 * i), day + " " + "02", patientId) != null) {
            temperatureMomentList.add(getTWDTO(2 + (24 * i), day + " " + "02", patientId));
        }
        if (getTWDTO(6 + (24 * i), day + " " + "06", patientId) != null) {
            temperatureMomentList.add(getTWDTO(6 + (24 * i), day + " " + "06", patientId));
        }
        if (getTWDTO(10 + (24 * i), day + " " + "10", patientId) != null) {
            temperatureMomentList.add(getTWDTO(10 + (24 * i), day + " " + "10", patientId));
        }
        if (getTWDTO(14 + (24 * i), day + " " + "14", patientId) != null) {
            temperatureMomentList.add(getTWDTO(14 + (24 * i), day + " " + "14", patientId));
        }
        if (getTWDTO(18 + (24 * i), day + " " + "18", patientId) != null) {
            temperatureMomentList.add(getTWDTO(18 + (24 * i), day + " " + "18", patientId));
        }
        if (getTWDTO(22 + (24 * i), day + " " + "22", patientId) != null) {
            temperatureMomentList.add(getTWDTO(22 + (24 * i), day + " " + "22", patientId));
        }
        return temperatureMomentList;
    }


    /**
     * 获取生命体征数据
     *
     * @param patientId
     * @param day
     * @param i
     * @return
     */
    private List<TemperatureMomentDTO> getTemperatureMomentDTOList(String patientId, String day, int i) {
        List<TemperatureMomentDTO> temperatureMomentList = new ArrayList<>();
        temperatureMomentList.add(getTemperatureMomentDTO(3 + (24 * i), day + " " + "03", patientId));

        temperatureMomentList.add(getTemperatureMomentDTO(7 + (24 * i), day + " " + "07", patientId));

        temperatureMomentList.add(getTemperatureMomentDTO(11 + (24 * i), day + " " + "11", patientId));

        temperatureMomentList.add(getTemperatureMomentDTO(15 + (24 * i), day + " " + "15", patientId));

        temperatureMomentList.add(getTemperatureMomentDTO(19 + (24 * i), day + " " + "19", patientId));

        temperatureMomentList.add(getTemperatureMomentDTO(23 + (24 * i), day + " " + "23", patientId));
        return temperatureMomentList;
    }

    /**
     * 获取生命体征中的血压的数据
     *
     * @param day
     * @param patientId
     * @return
     */
    private TemperatureMomentDTO getBloodPressure(String day, String patientId) {
        return new TemperatureMomentDTO(getVitalSign(0, day + " 12:00:00", day + " 00:00:00", "bp", patientId), getVitalSign(0, day + " 23:59:59", day + " 12:00:00", "bp", patientId));
    }

    /**
     * 获取生命体征中的呼吸的数据
     *
     * @param moment
     * @param dataTime
     * @param patientId
     * @return
     */
    private TemperatureMomentDTO getBrDTO(int moment, String dataTime, String patientId) {
        return new TemperatureMomentDTO(moment, getVitalSign(1, dataTime, null, "br", patientId), 0);
    }

    /**
     * 获取生命体征中的体温的数据
     *
     * @param moment
     * @param dataTime
     * @param patientId
     * @return
     */
    private TemperatureMomentDTO getTWDTO(int moment, String dataTime, String patientId) {
        if ("".equals(new TemperatureMomentDTO(moment, getVitalSign(1, dataTime, null, "temperature", patientId), 0).getTemperature())) {
            return null;
        }
        return new TemperatureMomentDTO(moment, getVitalSign(1, dataTime, null, "temperature", patientId), 0);
    }

    /**
     * 获取生命体征中的脉搏和心率的数据
     *
     * @param moment
     * @param dataTime
     * @param patientId
     * @return
     */
    private TemperatureMomentDTO getTemperatureMomentDTO(int moment, String dataTime, String patientId) {
        return new TemperatureMomentDTO(moment, getVitalSign(1, dataTime, null, "pulse", patientId), getVitalSign(1, dataTime, null, "hr", patientId));
    }

    /**
     * 获取生命体征中的值
     *
     * @param startTime
     * @param code
     * @param patientId
     * @return
     */
    private String getVitalSign(int i, String endTime, String startTime, String code, String patientId) {
        Map<String, Object> map = new HashMap<>();
        map.put("patientId", patientId);
        map.put("code", code);
        if (i == 0) {
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        } else {
            map.put("dataTime", endTime);
        }

        VitalSign vitalSign = findVitalSign(i, map);
        if (Objects.isNull(vitalSign)) {
            return "";
        }
        if (Objects.isNull(vitalSign.getContent())) {
            return "";
        }
        return vitalSign.getContent();
    }

    private VitalSign findVitalSign(int i, Map<String, Object> map) {
        if (i == 1) {
            return vitalSignDao.findTemperatureMomentVitalSign(map);
        }
        return vitalSignDao.findTemperatureNewestVitalSign(map);
    }

    public List<TemperatureMomentDTO> findTemperatureCodeData(String patientId, List<String> dateDayList, String code) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        List<String> timeList = new ArrayList<String>();
        for (String s : dateDayList) {
            timeList.add(s + " " + time1);
            timeList.add(s + " " + time2);
            timeList.add(s + " " + time3);
            timeList.add(s + " " + time4);
            timeList.add(s + " " + time5);
            timeList.add(s + " " + time6);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("patientId", patientId);
        map.put("code", code);
        map.put("list", timeList);
        List<TemperatureMomentDTO> maplist = vitalSignDao.findTemperatureMomentVitalSignByCode(map);
        if ("br".equals(code)) {
            for (int i = 0; i < timeList.size(); i++) {
                TemperatureMomentDTO dto = new TemperatureMomentDTO();
                String hours = "";
                String hr = "";
                for (int j = 0; j < maplist.size(); j++) {
                    TemperatureMomentDTO tDto = maplist.get(j);
                    if (timeList.get(i).equals(tDto.getTimes())) {
                        hr = tDto.getHr();
                    }
                }
                dto.setTemperature(hr);
                temperatureDayList.add(dto);
            }
        } else {
            for (int i = 0; i < timeList.size(); i++) {
                TemperatureMomentDTO dto = null;
                String hours = "";
                String hr = "";
                for (int j = 0; j < maplist.size(); j++) {
                    TemperatureMomentDTO tDto = maplist.get(j);
                    if (timeList.get(i).equals(tDto.getTimes())) {
                        dto = new TemperatureMomentDTO();
                        hours = tDto.getHours().replaceFirst("^0*", "");
                        hr = tDto.getHr();
                    }
                }
                if (dto != null) {
                    dto.setMoment(Integer.parseInt(hours) + 24 * (i / 6));
                    dto.setHr(hr);
                    temperatureDayList.add(dto);
                }
            }
        }


        return temperatureDayList;
    }

    /**
     * 心率
     *
     * @param patientId
     * @param dateDayList
     * @return
     */
    public List<TemperatureMomentDTO> findTemperatureHRData(String patientId, List<String> dateDayList) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        for (int i = 0; i < dateDayList.size(); i++) {
            String day = dateDayList.get(i);
            List<TemperatureMomentDTO> newList = getTemperatureMomentDTOAllList(patientId, day, i, "hr");
            if (newList != null && newList.size() > 0) {
                temperatureDayList.addAll(newList);
            }
        }
        return temperatureDayList;
    }

    /**
     * 脉搏
     *
     * @param patientId
     * @param dateDayList
     * @return
     */
    public List<TemperatureMomentDTO> findTemperaturePulseData(String patientId, List<String> dateDayList) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<TemperatureMomentDTO> temperatureDayList = new ArrayList<>();
        for (int i = 0; i < dateDayList.size(); i++) {
            String day = dateDayList.get(i);
            List<TemperatureMomentDTO> newList = getTemperatureMomentDTOAllList(patientId, day, i, "pulse");
            if (newList != null && newList.size() > 0) {
                temperatureDayList.addAll(newList);
            }
        }
        return temperatureDayList;
    }

    private List<TemperatureMomentDTO> getTemperatureMomentDTOAllList(String patientId, String day, int i, String code) {
        List<TemperatureMomentDTO> temperatureMomentList = new ArrayList<>();
        if (getTemperatureMomentDTOALL(2 + (24 * i), day + " " + "02", patientId, code) != null) {
            temperatureMomentList.add(getTemperatureMomentDTOALL(2 + (24 * i), day + " " + "02", patientId, code));
        }
        if (getTemperatureMomentDTOALL(6 + (24 * i), day + " " + "06", patientId, code) != null) {
            temperatureMomentList.add(getTemperatureMomentDTOALL(6 + (24 * i), day + " " + "06", patientId, code));
        }
        if (getTemperatureMomentDTOALL(10 + (24 * i), day + " " + "10", patientId, code) != null) {
            temperatureMomentList.add(getTemperatureMomentDTOALL(10 + (24 * i), day + " " + "10", patientId, code));
        }
        if (getTemperatureMomentDTOALL(14 + (24 * i), day + " " + "14", patientId, code) != null) {
            temperatureMomentList.add(getTemperatureMomentDTOALL(14 + (24 * i), day + " " + "14", patientId, code));
        }
        if (getTemperatureMomentDTOALL(18 + (24 * i), day + " " + "18", patientId, code) != null) {
            temperatureMomentList.add(getTemperatureMomentDTOALL(18 + (24 * i), day + " " + "18", patientId, code));
        }
        if (getTemperatureMomentDTOALL(22 + (24 * i), day + " " + "22", patientId, code) != null) {
            temperatureMomentList.add(getTemperatureMomentDTOALL(22 + (24 * i), day + " " + "22", patientId, code));
        }
        return temperatureMomentList;
    }

    private TemperatureMomentDTO getTemperatureMomentDTOALL(int moment, String dataTime, String patientId, String code) {

        if ("pulse".equals(code)) {
            if (getVitalSign(1, dataTime, null, "pulse", patientId) != "") {
                return new TemperatureMomentDTO(moment, getVitalSign(1, dataTime, null, "pulse", patientId), null);
            }
        } else {
            if (getVitalSign(1, dataTime, null, "hr", patientId) != "") {
                return new TemperatureMomentDTO(moment, null, getVitalSign(1, dataTime, null, "hr", patientId));
            }
        }
        return null;
    }


    /**
     * 获取spo2的每天的最大值
     *
     * @param patientId
     * @param dateDayList
     * @return
     */
    public List<String> findSpo2Data(List<String> dateDayList, String patientId) {
        errorIfEmpty(dateDayList, "查询时间集合不存在");
        List<String> temperatureDayList = new ArrayList<String>();
        for (String dataTime : dateDayList) {
            Map<String, Object> map = new HashMap<>();
            map.put("patientId", patientId);
            map.put("code", "spo2");
            map.put("dataTime", dataTime);
            int spo2Val = findTemperatureMomentVitalSignBySPO2(map);
            String spo2ValStr = "";
            if (spo2Val == 0) {
                spo2ValStr = "";
            } else {
                spo2ValStr = spo2Val + "";
            }
            temperatureDayList.add(spo2ValStr);
        }
        return temperatureDayList;
    }

    /****
     * 通过病人id和时间查询最大的spo2
     */
    public int findTemperatureMomentVitalSignBySPO2(Map<String, Object> map) {
        return vitalSignDao.findTemperatureMomentVitalSignBySPO2(map);
    }
}
